package com.wuyou.designPatterns.factory.abstractfactory.factory;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.ArrayList;
import java.util.List;

public abstract class Page {

    protected String title;

    protected String author;

    protected List<Item> content = new ArrayList<>();

    public Page(String title, String author) {
        this.title = title;
        this.author = author;
    }

    public void add(Item item) {
        this.content.add(item);
    }

    public void output() {
        try {
            String filename = title + ".html";
            String packageName = this.getClass().getPackage().getName().replace(".", File.separator) + File.separator;
            String foldName = System.getProperty("user.dir") + File.separator + "src" + File.separator + "main" + File.separator + "java" + File.separator + packageName + filename;
            System.out.println(foldName);
            File file = new File(foldName);
            if (file.createNewFile()) {
                System.out.println("文件创建成功：" + file.getAbsolutePath());
            }else {
                if(file.exists()){
                    file.delete();
                    file.createNewFile();
                }
            }
            Writer writer = new FileWriter(file);
            writer.write(this.makeHTML());
            writer.close();
            System.out.println("编写完成");
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public abstract String makeHTML();
}
